package com.sicau.socialpracticesystem.config.convert;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * @author zhaolinfeng3
 * @description:
 * @create: 2020-03-11 12:27
 **/
public class LocalDateTime2DateConverter implements Converter {
    private static final String DEFAULT_PATTERN = "yyyy-MM-dd";

    @Override
    public Class supportJavaTypeKey() {
        return null;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public LocalDateTime convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        //excel解析之后拿到的是日期的序列号
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DEFAULT_PATTERN);
        LocalDate localDate = LocalDate.parse(cellData.getStringValue(),dateTimeFormatter);
        LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.of(00,00));
        return localDateTime;
    }

    @Override
    public CellData convertToExcelData(Object o, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        LocalDateTime time = (LocalDateTime)o;
        String format = DateTimeFormatter.ofPattern(DEFAULT_PATTERN).format(time);
        return new CellData(format);
    }

}
